package gui_layer;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

@SuppressWarnings("serial")
public class MarkingCellRenderer extends DefaultTableCellRenderer
{
	
	private Set<Integer> incorrect_cells;
	private Set<Integer> warning_cells;
	private Set<Integer> notification_cells;
	
	public MarkingCellRenderer()
	{
		incorrect_cells = new TreeSet<Integer>();
		warning_cells = new TreeSet<Integer>();
		notification_cells = new TreeSet<Integer>();
	}
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
	    Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	    row = table.convertRowIndexToModel(row);
	    if(incorrect_cells.contains(row))
        {
	    	cell.setBackground(Color.RED);
	    	cell.setForeground(Color.BLACK);
        	
        } 
        else if(warning_cells.contains(row))
        {
        	cell.setForeground(Color.RED);
        	cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
        else if(notification_cells.contains(row))
        {
        	cell.setForeground(Color.ORANGE);
        	//cell.setBackground(Color.WHITE);
        }
        else
        {
        	cell.setForeground(Color.BLACK);
        	cell.setBackground(Color.WHITE);
        	cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
        return cell;
    }
	
	public void add_incorrect_cell(int cell_row)
	{
		incorrect_cells.add(cell_row);
	}
	
	public void remove_incorrect_cell(int cell_row)
	{
		incorrect_cells.remove(cell_row);
	}
	
	public void add_warning_cell(int cell_row)
	{
		warning_cells.add(cell_row);
	}
	
	public void remove_warning_cell(int cell_row)
	{
		warning_cells.remove(cell_row);
	}
	
	public void add_notification_cell(int cell_row)
	{
		notification_cells.add(cell_row);
	}
	
	public void remove_notification_cell(int cell_row)
	{
		notification_cells.remove(cell_row);
	}
	
	
	public void revalidate_after_delete(int cell_row)
	{
		revalidate_collection(warning_cells, cell_row);
		revalidate_collection(incorrect_cells, cell_row);
		revalidate_collection(notification_cells, cell_row);
	}
	
	private void revalidate_collection(Collection<Integer> coll, int cell_row)
	{
		List<Integer> revalidated_cells = new ArrayList<Integer>();
		Iterator<Integer> iterator = coll.iterator();
		while(iterator.hasNext())
		{
			int temp = iterator.next();
			if (temp == cell_row)
			{
				iterator.remove();
			}
			else if(temp > cell_row)
			{
				revalidated_cells.add(temp - 1);
				iterator.remove();
			}
		}
		coll.addAll(revalidated_cells);
	}
	

}
